<style>
  .header-font {
    font-size: 15px;
    font-weight: bold;
  }
  .line {
    margin: 10px 0;
    width: 100%;
    border: 0.5px solid rgb(218, 218, 218);
  }
  .table {
    margin-top: 10px;
  }
  .sale-rank-form {
    margin-bottom: -10px;
  }
  .paging {
    float:right;
    margin-top:10px;
  }
  .rank-btn-export {
    margin-bottom: 10px;
    /* margin-top: -46px; */
    /* float: right; */
  }
  .sale-tag {
    width: 180px;
    height: 50px;
    font-size: 13px;
    text-align: center;
    line-height: 50px;
    margin-left: 3px;
  }
  .cloud-sale-card {
    background: url(../../../assets/images/cloudSale.png) no-repeat;
    background-size:100% 100%;
  }
  .all-sale-card {
    background: url(../../../assets/images/allSale.png) no-repeat;
    background-size:100% 100%;
  }
  .month-sale-card {
    background: url(../../../assets/images/monthSale.png) no-repeat;
    background-size:100% 100%;
  }
  .customer-count-card {
    background: url(../../../assets/images/customerCount.png) no-repeat;
    background-size:100% 100%;
  }
  .come-count-card {
    background: url(../../../assets/images/comeCount.png) no-repeat;
    background-size:100% 100%;
  }
  .sign-count-card {
    background: url(../../../assets/images/signCount.png) no-repeat;
    background-size:100% 100%;
  }
  .container {
    clear: both;
    display: flex;
    justify-content: space-around;
    margin-bottom:10px;
  }
  .sale-card-text {
    /* width: 120px; */
    /* text-align: center; */
    margin-left: 13%;
    margin-top: 25px;
    /* left: 55%; */
    color: #fff;
  }
</style>
<template>
  <div>
    <div class='header-font'>
      <Icon type="md-apps" />
      <span>销售排名</span>
    </div>
    <div class="line"></div>
    <sale-rank-form
      class="sale-rank-form"
      @on-success-valid="handleSubmit">
    </sale-rank-form>
    <!-- <div>
      <div style="font-size: 15px; margin-left: 10px;">销售汇总数据：</div>
      <div style="margin-left: 130px; margin-top: -28px;">
        <Tag color="blue" class="sale-tag">云销售人数：{{ saleView && saleView.salenum? saleView.salenum : '0' }}</Tag>
        <Tag color="blue" class="sale-tag">总销售业绩：{{saleView && saleView.aprice ? saleView.aprice : '0'}}</Tag>
        <Tag color="blue" class="sale-tag">当月销售业绩：{{saleView && saleView.mprice ? saleView.mprice : '0'}}</Tag>
        <Tag color="blue" class="sale-tag">客户人数：{{saleView && saleView.allnum ? saleView.allnum : '0'}}</Tag>
        <Tag color="blue" class="sale-tag">到店人数：{{saleView && saleView.cnum ? saleView.cnum : '0'}}</Tag>
        <Tag color="blue" class="sale-tag">签约人数：{{saleView && saleView.onum ? saleView.onum : '0'}}</Tag>
      </div>
    </div> -->
    <div class="container">
      <div class="cloud-sale-card" style="width: 246px; height: 122px">
        <div class="sale-card-text">
          <span style="font-size: 17px;">
          云销售人数(人)
          </span>
          <div style="font-size: 30px;">{{saleView && saleView.salenum? saleView.salenum : '0'}}</div>
        </div>
      </div>
      <div class="all-sale-card" style="width: 246px; height: 122px">
        <div class="sale-card-text">
          <span style="font-size: 17px;">
          总销售业绩(元)
          </span>
          <div style="font-size: 30px;">{{saleView && saleView.aprice ? saleView.aprice : '0'}}</div>
        </div>
      </div>
      <div class="month-sale-card" style="width: 246px; height: 122px">
        <div class="sale-card-text">
          <span style="font-size: 17px;">
          当月销售业绩(元)
          </span>
          <div style="font-size: 30px;">{{saleView && saleView.mprice ? saleView.mprice : '0'}}</div>
        </div>
      </div>
      <div class="customer-count-card" style="width: 246px; height: 122px">
        <div class="sale-card-text">
          <span style="font-size: 17px;">
          意向人数(人)
          </span>
          <div style="font-size: 30px;">{{saleView && saleView.allnum ? saleView.allnum : '0'}}</div>
        </div>
      </div>
      <div class="come-count-card" style="width: 246px; height: 122px">
        <div class="sale-card-text">
          <span style="font-size: 17px;">
          到店人数(人)
          </span>
          <div style="font-size: 30px;">{{saleView && saleView.cnum ? saleView.cnum : '0'}}</div>
        </div>
      </div>
      <div class="sign-count-card" style="width: 246px; height: 122px">
        <div class="sale-card-text">
          <span style="font-size: 17px;">
          签约人数(人)
          </span>
          <div style="font-size: 30px;">{{saleView && saleView.onum ? saleView.onum : '0'}}</div>
        </div>
      </div>
    </div>
    <div>
      <Tabs type="card" style="width: 100%" @on-click="handleClickTab">
        <TabPane label="列表" name="列表">
          <btn-export :tableData="rankList" :tableTitle="columns" :title="title" class="rank-btn-export"></btn-export>
          <Table
            class="table"
            :columns="columns"
            :data="historyData"
            :loading="loading"
            border>
          </Table>
          <table-page
            class="paging"
            :dataCount="dataCount"
            @change-page="changePage"
            @change-page-size="changePageSize"
            :pageSize="pageSize">
          </table-page>
        </TabPane>
        <TabPane label="当月销售业绩排名" name="当月销售业绩排名">
          <sale-bar v-if="showBar" style="height:500px" :value="monthSaleRankList" :name="saleRankName" :color="saleRankColor" :text="saleRankText" />
        </TabPane>
        <TabPane label="意向客户排名" name="意向客户排名">
          <sale-bar v-if="showBar" style="height:500px" :value="wantOrderRankList" :name="wantRankName" :color="wantRankColor" :text="wantRankText" />
        </TabPane>
        <TabPane label="到店客户排名" name="到店客户排名">
          <sale-bar v-if="showBar" style="height:500px" :value="comeOrderRankList" :name="comeRankName" :color="comeRankColor" :text="comeRankText" />
        </TabPane>
        <TabPane label="签约客户排名" name="签约客户排名">
          <sale-bar v-if="showBar" style="height:500px" :value="sellOrderRankList" :name="signRankName" :color="signRankColor" :text="signRankText" />
        </TabPane>
        <TabPane label="未成交客户排名" name="未成交客户排名">
          <sale-bar v-if="showBar" style="height:500px" :value="unsellOrderRankList" :name="unSignRankName" :color="unSignRankColor" :text="unSignRankText" />
        </TabPane>
      </Tabs>
    </div>
  </div>
</template>
<script>
import {
  mapActions,
  mapState
} from 'vuex'
import SaleRankForm from '_c/sale-rank-form'
import SaleBar from '_c/sale-bar'
import BtnExport from '_c/btn-export'
import TablePage from '_c/table-page'
export default {
  components: {
    SaleRankForm,
    TablePage,
    BtnExport,
    SaleBar
  },
  data () {
    return {
      page: 1,
      title: '销售管理销售排名',
      loading: true,
      saleRankName: '销量',
      saleRankColor: 'rgba(248, 187, 18, 0.945)',
      saleRankText: '当月销售排名',
      wantRankName: '销量',
      wantRankColor: 'yellow',
      wantRankText: '意向客户排名',
      comeRankName: '销量',
      comeRankColor: 'red',
      comeRankText: '到店客户排名',
      signRankName: '销量',
      signRankColor: 'rgba(44, 132, 248, 0.945)',
      signRankText: '签约客户排名',
      unSignRankName: '销量',
      unSignRankColor: 'rgba(248, 125, 54, 0.945)',
      unSignRankText: '未成交客户排名',
      rows: 20,
      showBar: false,
      ctime: this.$moment(new Date()).format('YYYY-MM'),
      confirmData: {},
      playload: { ctime: this.$moment(new Date()).format('YYYY-MM') },
      postData: {},
      columns: [
        {
          title: '序号',
          type: 'index2',
          width: 100,
          align: 'center',
          render: (h, params) => {
            return h('span', params.index + (this.page - 1) * this.pageSize + 1)
          }
        },
        {
          title: '签约销售',
          key: 'uname',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '加入时间',
          key: 'ctime',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '当月销售金额',
          key: 'mprice',
          minWidth: 150,
          align: 'center'
          // render: (h, params) => {
          //   return h('span', params.row.mprice ? params.row.mprice : '-')
          // }
        },
        {
          title: '客户人数',
          key: 'acn',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '到店人数',
          key: 'cnum',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '签约人数',
          key: 'onum',
          minWidth: 150,
          align: 'center'
        },
        {
          title: '未成交客户',
          key: 'unum',
          minWidth: 150,
          align: 'center'
        }
        // {
        //   title: '操作',
        //   key: 'cz',
        //   // width: 200,
        //   align: 'center',
        //   render: (h, params) => {
        //     return h('div', [
        //       h('Button', {
        //         props: {
        //           type: 'primary',
        //           size: 'small',
        //           ghost: 'ghost'
        //         },
        //         style: {
        //           marginRight: '5px'
        //         },
        //         on: {
        //           click: () => {
        //             this.handleClick(params.row.id, params.row)
        //           }
        //         }
        //       }, '详情')
        //     ])
        //   }
        // }
      ]
    }
  },
  computed: {
    ...mapState({
      rankList: state => state.saleRank.rankList,
      saleView: state => state.sale.saleView,
      dataCount: state => state.saleRank.rankDataCount,
      historyData: state => state.saleRank.rankHistoryData,
      pageSize: state => state.saleRank.rankPageSize,
      row: state => state.saleRank.row,
      orderRateList: state => state.order.orderRateList,
      monthSaleRankList: state => state.order.monthSaleRankList,
      wantOrderRankList: state => state.order.wantOrderRankList,
      comeOrderRankList: state => state.order.comeOrderRankList,
      sellOrderRankList: state => state.order.sellOrderRankList,
      unsellOrderRankList: state => state.order.unsellOrderRankList
    })
  },
  methods: {
    ...mapActions([
      'getRankList',
      'getOrderRateList',
      'getMonthSaleRankList',
      'getWantOrderRankList',
      'getComeOrderRankList',
      'getSellOrderRankList',
      'getUnsellOrderRankList',
      'getSaleViewData',
      'getRankCurrentRow',
      'editRankData',
      'changeRankPage',
      'changeRankPageSize'
    ]),
    handleSubmit (playload) {
      this.loading = true
      // console.log(playload)
      this.playload = playload
      this.getRankList({
        ...playload,
        rows: this.rows
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
      this.getSaleViewData({
        ctime: playload.ctime
      })
    },
    // handleClick (id, row) {
    //   let newRow = {}
    //   this.id = id
    //   if (row) {
    //     newRow = Object.assign({}, row)
    //   }
    //   this.getSaleDrawerList({ uid: row.id ? row.id : -1 })
    //   this.getSaleCurrentRow(newRow)
    //   this.updateSaleDrawerState(true)
    // },
    handleClickTab (name) {
      // console.log(name)
      if (name === '列表') {
        this.loading = true
        this.getRankList({
          ...this.playload,
          rows: this.rows
        }).then((res) => {
          if (res.data.state === '000') {
            this.loading = false
          }
        })
      }
      if (name === '当月销售业绩排名') {
        this.showBar = false
        this.getMonthSaleRankList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showBar = true
          }
        })
      }
      if (name === '意向客户排名') {
        this.showBar = false
        this.getWantOrderRankList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showBar = true
          }
        })
      }
      if (name === '到店客户排名') {
        this.showBar = false
        this.getComeOrderRankList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showBar = true
          }
        })
      }
      if (name === '签约客户排名') {
        this.showBar = false
        this.getSellOrderRankList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showBar = true
          }
        })
      }
      if (name === '未成交客户排名') {
        this.showBar = false
        this.getUnsellOrderRankList({
          rows: 10,
          page: 1
        }).then((res) => {
          if (res.data.state === '000') {
            this.showBar = true
          }
        })
      }
    },
    changePage (index) {
      this.loading = true
      this.page = index
      this.changeRankPage(index)
      this.getRankList({
        page: index,
        rows: this.rows,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    },
    changePageSize (value) {
      this.loading = true
      this.rows = value
      this.changeRankPageSize(value)
      this.changePage(this.page)
      this.getRankList({
        rows: value,
        ...this.playload
      }).then((res) => {
        if (res.data.state === '000') {
          this.loading = false
        }
      })
    }
  },
  created () {
    this.getRankList({
      ctime: this.ctime
    }).then((res) => {
      if (res.data.state === '000') {
        this.loading = false
      }
    })
    this.getSaleViewData({
      ctime: this.playload.ctime
    })
  }
}
</script>
